package tp.pr5;

import tp.pr5.items.Item;
import tp.pr5.items.ItemContainer;

/**
 * It represents a place in the city. Places are connected by streets according to the 4 compass directions: North, East, South and West. Every place has a name and a textual description about itself. This description is displayed when the robot arrives at the place.
 *  A place can represent the spaceship where the robot is safe. When the robot arrives at this place, the robot shuts down and the application will finish.
 *
 */
public class Place implements PlaceInfo
{
	private String name;
	private String description;
	private boolean spaceShip;
	
	private ItemContainer itemContainer;
	
	
	/**
	 * Creates the place
	 * @param name
	 * @param isSpaceShip
	 * @param description
	 * 
	 * @return Place
	 */
	
	public Place(String name, boolean isSpaceShip, String description)
	{
		this.name = name;
		this.spaceShip = isSpaceShip;
		this.description = description;
		this.itemContainer = new ItemContainer();
	}
	
	/**
	 * Overrides toString method. Returns the place name and its description
	 * 
	 * @return String
	 */
	
	public String toString()
	{
		String infoPlace = this.name + Constants.LINE_SEPARATOR + this.description;
		String items = "";
		
		if(this.itemContainer.numberOfItems() > 0)
		{
			items = "The place contains these objects:" + Constants.LINE_SEPARATOR;
			items = items + this.itemContainer.toString();
		}
		else
		{
			items = "The place is empty. There are no objects to pick" + Constants.LINE_SEPARATOR;
		}
		
		return  infoPlace + Constants.LINE_SEPARATOR + items;
	}

	/**
	 * Tries to pick an item characterized by a given identifier from the place. 
	 * If the action was completed the item must be removed from the place. 
	 *
	 * @param id - The identifier of the item
	 * 
	 * @return The item of identifier id if it exists in the place. Otherwise the method returns null
	 */
	
	public Item pickItem(String id)
	{
		return this.itemContainer.pickItem(id);
	}
	
	/**
	 * Tries to add an item to the place. 
	 * The operation can fail (if the place already contains an item with the same name)
	 * 
	 * @param it - The item to be added
	 * 
	 * @return true if and only if the item can be added to the place, i.e., the place does not contain an item with the same name
	 */
	
	public boolean addItem(Item it) 
	{
		return this.itemContainer.addItem(it);
	}
	
	/**
	 * Gets the string from the place
	 * 
	 * @return A String that represents the place
	 */

	public String getPlace()
	{
		return this.name;
	}
	
	/**
	 * Gets the ItemContainer from the place
	 * 
	 * @return A ItemContainer that represents the ItemContainer
	 */
	
	public ItemContainer getItems()
	{
		return this.itemContainer;
	}
	
	/**
	 * Checks if an item is in this place
	 * 
	 * @param id  Identifier of an item
	 * @return true if and only if the place contains the item identified by id
	 */
	
	public boolean existItem(String id)
	{
		Item it = this.itemContainer.getItem(id); 
		
		if(it != null)
		{
			return true;
		}
	
		return false;
	}
	
	/**
	 * Drop an item in this place. The operation can fail, returning false
	 * 
	 * @param it The name of the item to be dropped.
	 * @return true if and only if the item is dropped in the place, i.e., an item with the same identifier does not exists in the place
	 */
	
	public boolean dropItem(Item it)
	{
		if(!this.existItem(it.getId()))
		{
			this.itemContainer.addItem(it);
			return true;
		}
		return false;
	}
	
	/**
	 * Displays description of the place
	 * 
	 * @return The place description
	 */
	
	@Override
	public String getDescription()
	{
		return this.description + Constants.LINE_SEPARATOR + this.itemContainer.toString();
	}

	/**
	 * Return the place name
	 * 
	 * @return The place name
	 */
	@Override
	public String getName()
	{
		return this.name;
	}
	
	/**
	 * Return true if the place represents a spaceship.
	 * 
	 * @return boolean true if the place represents a space ship
	 */
	@Override
	public boolean isSpaceship()
	{
		return this.spaceShip;
	}
	
	public Item getItem(String id)
	{
		return this.itemContainer.getItem(id);
	}
	
	public int storm()
	{
		return 50;
	}

	
}
